package com.org.testproject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class AssetsTest extends Activity{
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		TextView textView = new TextView(this);
		StringBuilder builder = new StringBuilder();
		
		AssetManager assetManager = getAssets();
		InputStream inputStream = null;
		
		try
		{
			inputStream = assetManager.open("texts/log.txt");
			String text = loadTextFile(inputStream);
			textView.setText(text);
		}
		catch(Exception e)
		{
			textView.setText("Couldn't load file");
		}
		finally
		{
			if(inputStream != null)
			{
				try
				{
					inputStream.close();
				}
				catch(Exception e)
				{
					textView.setText("Couldn't close file");
				}
			}
		}
		
		setContentView(textView);
	}
	
	public String loadTextFile(InputStream inputStream) throws IOException
	{
		ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
		byte[] bytes = new byte[4096];
		int len = 0;
		
		while((len = inputStream.read(bytes)) > 0)
		{
			byteStream.write(bytes, 0, len);
		}
		return new String(byteStream.toByteArray(), "UTF8");
	}
}
